package com.wanbangee.marksystem.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import com.wanbangee.SSO.JwtUtil;
import com.wanbangee.marksystem.service.UAccountService;
import com.wanbangee.marksystem.service.UTeacherService;

import com.wanbangee.marksystem.entities.UAccount;
import com.wanbangee.marksystem.entities.UTeacher;

@RestController
public class UAccountController {
	@Autowired
	private UAccountService uAccountService;
    @Autowired
    private UTeacherService uTeacherService;
   /* @Autowired
    private RedisTemplate<String,Object> redisTemplate;*/

	@PostMapping(value = "/login")
	public Map<String, Object> login(String loginName, String loginPass,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		String username = loginName;
		String password = loginPass;
		// 账号密码验证
		UAccount uAccount = uAccountService.selectAccountByLoginNameAndLoginPass(loginName, loginPass);
		if (null != uAccount) {
			// 验证成功后发送token
			String token = JwtUtil.sign(username, password);
			if (token != null) {
				UTeacher uTeacher= this.uTeacherService.selectTeacherByaccId(uAccount.getAccId());
				map.put("code", "200");
				map.put("message", "认证成功");
				map.put("token", token);
				
				
				/*Map<String, Object> redismap = new HashMap<>();
				redismap.put("loginName", uAccount.getLoginName());
				redismap.put("accId", uAccount.getAccId());
				redismap.put("schId", uAccount.getSchId());
				redismap.put("schNm", uAccount.getSchNm());
				//redismap.put("loginPass", uAccount.getLoginPass());				
				redismap.put("teaNm",uTeacher.getTeaNm());
				redismap.put("teaNo",uTeacher.getTeaNo());
				redismap.put("teaId",uTeacher.getTeaId());
				redismap.put("teaEmail",uTeacher.getTeaEmail());
				redismap.put("teaState",uTeacher.getTeaState());
				//声明的方式写入redis
				 redisTemplate.opsForHash().putAll(token, redismap);
				 //给hash设置过期时间
				 redisTemplate.expire(token, 2*60, TimeUnit.MINUTES);	*/			
				
				return map;
			}
		}
		map.put("code", "403");
		map.put("message", "认证失败");
		return map;

	}
	
	@PostMapping("/insertAccount")
	public Map<String, Object> insertTeacherAccount(UAccount uAccount){
		
		Map<String, Object> map = new HashMap<>();
		map.put("accId", uAccountService.insertTeacherAccount(uAccount));
		map.put("code", 1001);
		map.put("msg", "新增成功");
		return map;
	}

	/*@GetMapping("getUAccountInfo")
	public Map<Object, Object> getUserInfo(@RequestHeader(value="token") String token){
		//System.out.println(token);
		//System.out.println( redisTemplate.getExpire(token, TimeUnit.SECONDS));
		Map<Object, Object> entries = redisTemplate.opsForHash().entries(token);
		return entries;
	}*/
	
	@PutMapping("/updateLoginPass")
	public Map<String, Object> updateLoginPassByaccIdAndloginName(Integer accId,String loginName,String newloginPass,String oldloginPass){
		Map<String, Object> map = new HashMap<>();
		Integer i=this.uAccountService.updateLoginPassByaccIdAndloginName(accId, loginName,newloginPass, oldloginPass);
		if(i==1) {
			map.put("code", 1001);
			map.put("msg", "修改成功");
			return map;
		}
		map.put("code", 1002);
		map.put("msg", "修改失败");
		return map;
	}
	
	
}